<?php
    class member{
        private $memID;
        private $database;

        public function __construct() {
            require_once dirname(__FILE__)."/../lib/database.inc.php";
            $dbObj=new database;
            $this->database=$dbObj->connect();
                    
            $args = func_get_args();
            if(count($args)==1){
                $this->setMemID($args[0]);
            }
        }

        private function setMemID($memID){
            $this->memID=$memID;
        }
        public function getMemID(){
            return $this->memID;
        }

        function getMember(){
            $query="
                select  *
                from    member
                where   memID='".$this->memID."'
                ";
            $result=  $this->database->query($query);
            $row=  $result->fetch_array(MYSQLI_ASSOC);
            return $row;
        }
        
        function availableUsername($user,$memID=""){
            if($user==""){
                return TRUE;
            }else if($memID==""){
                $query="
                    select  *
                    from    member
                    where   user='".$user."'
                    ";
                $result=  $this->database->query($query);
                if($result->num_rows==0){
                    return TRUE;
                }else{
                    return FALSE;
                }
            }else{
                $query="
                    select  *
                    from    member
                    where   user='".$user."' and
                            memID<>'".$memID."'
                    ";
                $result=  $this->database->query($query);
                if($result->num_rows==0){
                    return TRUE;
                }else{
                    return FALSE;
                }
            }
        }

        function addMember($memberArr){
            $this->database=  connect();
            if(isset($memberArr["private"])){
                $private=$memberArr["private"];
            }else{
                $private="1";
            }
            $query="
                insert into member
                    (
                        cardID,
                        user,
                        pass,
                        title,
                        firstName,
                        lastName,
                        mobile,
                        geoLocation,
                        coopID,
                        private
                    ) values (
                        '".$memberArr["cardID"]."',
                        '".$memberArr["user"]."',
                        '".md5($memberArr["pass"])."',
                        '".$memberArr["title"]."',
                        '".$memberArr["firstName"]."',
                        '".$memberArr["lastName"]."',
                        '".$memberArr["mobile"]."',
                        '".$memberArr["geoLocation"]."',
                        '".$memberArr["coopID"]."',
                        '".$private."'
                    )
                ";
            $this->database->query($query);
            return $this->database->insert_id;
        }
       
        public function  updateMember($input){
            $this->database=  connect();
            if(isset($input["private"])){
                $private=$input["private"];
            }else{
                $private="1";
            }
            if($input["user"]=="" && $input["pass"]==""){
                $query="
                    update  member
                    set     cardID='".$input["cardID"]."',
                            title='".$input["title"]."',
                            firstName='".$input["firstName"]."',
                            lastName='".$input["lastName"]."',
                            mobile='".$input["mobile"]."',
                            geoLocation='".$input["geoLocation"]."',
                            coopID='".$input["coopID"]."',
                            private='".$private."'
                    where   memID='".$this->getMemID()."'
                    ";
            }else if($input["pass"]==""){
                $query="
                    update  member
                    set     cardID='".$input["cardID"]."',
                            user='".$input["user"]."',
                            title='".$input["title"]."',
                            firstName='".$input["firstName"]."',
                            lastName='".$input["lastName"]."',
                            mobile='".$input["mobile"]."',
                            geoLocation='".$input["geoLocation"]."',
                            coopID='".$input["coopID"]."',
                            private='".$private."'
                    where   memID='".$this->getMemID()."'
                    ";
            }else{
                $query="
                    update  member
                    set     cardID='".$input["cardID"]."',
                            user='".$input["user"]."',
                            pass='".md5($input["pass"])."',
                            title='".$input["title"]."',
                            firstName='".$input["firstName"]."',
                            lastName='".$input["lastName"]."',
                            mobile='".$input["mobile"]."',
                            geoLocation='".$input["geoLocation"]."',
                            coopID='".$input["coopID"]."',
                            private='".$private."'
                    where   memID='".$this->getMemID()."'
                    ";
            }
            $this->database->query($query);
            return $this->getMember();
        }
        
        public function deleteMember($memID=""){
            if($memID==""){
                $memID=  $this->memID;
            }
            
            $query="
                delete from member_farm_crop
                where       farmID in (
                                select  farmID
                                from    member_farm
                                where   memID='".$memID."'
                            )
                ";
            $this->database->query($query);
            
            $query="
                delete from member_farm
                where       memID='".$memID."'
                ";
            $this->database->query($query);
            
            $query="
                delete from member
                where       memID='".$memID."'
                ";
            $this->database->query($query);
            
            return TRUE;
        }
        
        public function memName(){
            require_once dirname(__FILE__).'/../function/functionPHP.php';
            if(ofAuthenticated()){
                $member=$this->getMember();
                return $member["title"]." ".$member["firstName"]." ".$member["lastName"];
            }else{
                if(memAuthenticated()){
                    if($_SESSION["memAuthen"]["memID"]==$this->memID){
                        $member=$this->getMember();
                        return $member["title"]." ".$member["firstName"]." ".$member["lastName"];
                    }else{
                        $member=$this->getMember();
                        if($member["private"]=="0"){
                            return $member["title"]." ".$member["firstName"]." ".$member["lastName"];
                        }else{
                            return "เกษตรกร";
                        }
                    }
                }
            }
        }
    }
 